﻿using System;
using System.Windows.Forms;
using System.Drawing;

namespace mo
{
    class MoAppContext : ApplicationContext
    {
        public const string NOMBRE = "mo v0.1";
        NotifyIcon notifyIcon = new NotifyIcon();
        Timer timer = new Timer();
        ContextMenuStrip menu = new ContextMenuStrip();

        bool togle = false;

        public MoAppContext()
        {
            Application.ApplicationExit += new EventHandler(Application_ApplicationExit);
            InitializeComponent();
            notifyIcon.Visible = true;

            timer_Tick(null, null);
        }

        void Application_ApplicationExit(object sender, EventArgs e)
        {
            notifyIcon.Visible = false;
            notifyIcon.Dispose();
        }

        void menuSalir(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show(
                "¿Salir?", 
                MoAppContext.NOMBRE, 
                MessageBoxButtons.YesNo, 
                MessageBoxIcon.Question
            );

            if (DialogResult.Yes == dr)
                Application.Exit();
        }

        void menuMO(object sender, EventArgs e)
        {
            menu.Items[0].Enabled = false;
            FrmMO frmMO = new FrmMO();
            frmMO.ShowDialog();
            menu.Items[0].Enabled = true;
        }

        void InitializeComponent()
        {
            menu.Items.Add(new ToolStripMenuItem(NOMBRE, null, menuMO));   
            menu.Items.Add("-");
            menu.Items.Add(new ToolStripMenuItem("Salir", null, menuSalir));

            notifyIcon.ContextMenuStrip = menu;
            
            crearIcono1();
            crearIcono2();

            timer.Interval = 100;
            timer.Tick += new EventHandler(timer_Tick);

            timer.Enabled = true;
        }

        void crearIcono1()
        {
            Bitmap bmp = new Bitmap(16, 16);
            Graphics g = Graphics.FromImage(bmp);
            //g.FillRegion(new SolidBrush(Color.Black), new Region(new Rectangle(0, 0, 15, 15)));
            g.FillRectangle(new SolidBrush(Color.Black), new Rectangle(0, 0, 15, 15));
            g.DrawString("27", new Font("Arial", 8), new SolidBrush(Color.Lime), new PointF(0, 0));
 
            //g.Flush();
            bmp.Save("1.ico");
            g.Dispose();
            bmp.Dispose();
            bmp = null;
        }

        private void crearIcono2()
        {
            Bitmap bmp = new Bitmap(16, 16);
            Graphics g = Graphics.FromImage(bmp);
            g.FillRectangle(new SolidBrush(Color.Lime), new Rectangle(0, 0, 15, 15));
            g.DrawString("27", new Font("Arial", 8), new SolidBrush(Color.Black), new PointF(0, 0));

            //g.Flush();
            bmp.Save("2.ico");
            g.Dispose();
            bmp.Dispose();
            bmp = null;
        }

        void timer_Tick(object sender, EventArgs e)
        {
            togle = !togle;

            if (togle)
            {
                Icon viejo = notifyIcon.Icon;

                Bitmap mapa = (Bitmap)Bitmap.FromFile("2.ico");
                Icon icono = Converter.BitmapToIcon(mapa);
                notifyIcon.Icon = icono;
                mapa.Dispose();

                if (viejo != null)
                {
                    viejo.Dispose();
                    viejo = null;
                }
            }
            else
            {
                Icon viejo = notifyIcon.Icon;

                Bitmap mapa = (Bitmap)Bitmap.FromFile("1.ico");
                Icon icono = Converter.BitmapToIcon(mapa);
                notifyIcon.Icon = icono;
                mapa.Dispose();

                if (viejo != null)
                {
                    viejo.Dispose();
                    viejo = null;
                }
            }
        }
    }
}
